<?php 
Class Admin {
	var $html = '';
	
	function __construct() {}
	
	function html() {
		$this->loadWindow();
		return $this->html;
	}
	
	static function createWindow($title, $content, $class='', $menu=''){
		global $_SITE, $_TAG_REPLACE;
		
		if ($class != ''){
			$class = ' '.$class;
		}
		
		$html = file_get_contents($_SITE['path']['dir']['blocks']['container'].'/admin/admin-window.tag.html');
		$html = str_replace($_TAG_REPLACE["admin-window-class"], $class, $html);
		$html = str_replace($_TAG_REPLACE["admin-window-title"], $title, $html);
		$html = str_replace($_TAG_REPLACE["admin-window-content"], $content, $html);
		$html = str_replace($_TAG_REPLACE["admin-menu"], $menu, $html);
		
		return $html;		
	}
	
	private function loadWindow() {
		global $_SITE, $_USER, $_TAG_REPLACE;
		if ($_SITE['admin']['activate']) {
			$this->html = file_get_contents($_SITE['path']['dir']['blocks']['container'].'/admin/admin.tag.html');
			
			if ($_USER->login) {
				/*
				 * title window admin
				 * */
				$strWelcome = '{VOCABULARY|hi} '.$_USER->get('nick');
				$htmlLoginTag = file_get_contents($_SITE['path']['dir']['blocks']['container'].'/admin/admin-loged.tag.html');
				$htmlLoginTag = str_replace($_TAG_REPLACE["admin-loged"], $strWelcome, $htmlLoginTag);
				$this->html = str_replace($_TAG_REPLACE["admin-login"], $htmlLoginTag, $this->html);
				
				/*
				 * menu admin
				 * */
				$objMenu = simplexml_load_file($_SITE['path']['file']['admin']['menu']);
				$menu = self::loadMenu($objMenu);
				$this->html = str_replace($_TAG_REPLACE["admin-menu"], $menu, $this->html);
				
			} else {
				$htmlLoginTag = file_get_contents($_SITE['path']['dir']['blocks']['container'].'/admin/admin-login.tag.html');
				$this->html = str_replace($_TAG_REPLACE["admin-login"], $htmlLoginTag, $this->html);
			}
		}
	}
	
	private static function loadMenu($treeMenu, $res = '') {
		global $_SITE, $_TAG_REPLACE, $_USER;
		
		if ($res == '') {
			$res = '<ul class="dropdown">';
		} else {
			$res = '<ul>';
		}
				
		foreach($treeMenu->item as $item) {
			$class = (String)$item->class;
			$href = (String)$item->href;
			$title = (String)$item->label;
			$label = (String)$item->label;
			$type = (String)$item->type;
			$permission = (String)$item->permission;
			
			if ($_USER->asPermission($permission) || empty($permission)) {
				
				$htmlItemMenu = file_get_contents($_SITE['path']['dir']['blocks']['container'].'/admin/admin-item-menu.tag.html');
				$htmlItemMenu = str_replace($_TAG_REPLACE["admin-menu-class"], $class, $htmlItemMenu);
				$htmlItemMenu = str_replace($_TAG_REPLACE["admin-menu-href"], $href, $htmlItemMenu);
				$htmlItemMenu = str_replace($_TAG_REPLACE["admin-menu-href-title"], $title, $htmlItemMenu);
				$htmlItemMenu = str_replace($_TAG_REPLACE["admin-menu-href-class"], $type, $htmlItemMenu);
				$htmlItemMenu = str_replace($_TAG_REPLACE["admin-menu-href-label"], $label, $htmlItemMenu);
				
				if (!empty($item->submenu)) {
					$sub = self::loadMenu($item->submenu, $res);
					$htmlItemMenu = str_replace($_TAG_REPLACE["admin-menu-sub"], $sub, $htmlItemMenu);
				} else {
					$htmlItemMenu = str_replace($_TAG_REPLACE["admin-menu-sub"], '', $htmlItemMenu);
				}
				
				$res .= $htmlItemMenu;
			}
		}
		
		$res .= '</ul>';
		return $res;
	}
	
	public function getAllUsers(){
		global $_DB;
		
		$sql = "SELECT id, name, surname, nick FROM ".$_DB->getPrefix()."users ORDER BY name ASC, surname ASC";
		return $_DB->query($sql);
	}
}
?>